package cn.iocoder.yudao.module.wine.service.supplyorderitem;

import cn.hutool.core.collection.CollectionUtil;
import cn.iocoder.yudao.module.system.api.dict.DictDataApi;
import cn.iocoder.yudao.module.system.api.dict.dto.DictDataRespDTO;
import cn.iocoder.yudao.module.wine.controller.app.supplyorderitem.vo.AppSupplyOrderItemRespVO;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import java.util.stream.Collectors;

import cn.iocoder.yudao.module.wine.controller.admin.supplyorderitem.vo.*;
import cn.iocoder.yudao.module.wine.dal.dataobject.supplyorderitem.SupplyOrderItemDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.wine.dal.mysql.supplyorderitem.SupplyOrderItemMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.wine.enums.ErrorCodeConstants.*;

/**
 * 订单项信息 Service 实现类
 *
 * @author 管理员
 */
@Service
@Validated
public class SupplyOrderItemServiceImpl implements SupplyOrderItemService {

    @Resource
    private SupplyOrderItemMapper supplyOrderItemMapper;

    @Resource
    private DictDataApi dataApi;


    @Override
    public Long createSupplyOrderItem(SupplyOrderItemSaveReqVO createReqVO) {
        // 插入
        SupplyOrderItemDO supplyOrderItem = BeanUtils.toBean(createReqVO, SupplyOrderItemDO.class);
        supplyOrderItemMapper.insert(supplyOrderItem);
        // 返回
        return supplyOrderItem.getId();
    }

    @Override
    public void updateSupplyOrderItem(SupplyOrderItemSaveReqVO updateReqVO) {
        // 校验存在
        validateSupplyOrderItemExists(updateReqVO.getId());
        // 更新
        SupplyOrderItemDO updateObj = BeanUtils.toBean(updateReqVO, SupplyOrderItemDO.class);
        supplyOrderItemMapper.updateById(updateObj);
    }

    @Override
    public void deleteSupplyOrderItem(Long id) {
        // 校验存在
        validateSupplyOrderItemExists(id);
        // 删除
        supplyOrderItemMapper.deleteById(id);
    }

    private void validateSupplyOrderItemExists(Long id) {
        if (supplyOrderItemMapper.selectById(id) == null) {
            throw exception(INFO_NOT_EXISTS);
        }
    }

    @Override
    public SupplyOrderItemDO getSupplyOrderItem(Long id) {
        return supplyOrderItemMapper.selectById(id);
    }

    @Override
    public PageResult<SupplyOrderItemDO> getSupplyOrderItemPage(SupplyOrderItemPageReqVO pageReqVO) {
        return supplyOrderItemMapper.selectPage(pageReqVO);
    }

    @Override
    public List<SupplyOrderItemRespVO> getAdminSupplyOrderItemList(Long orderId) {
        List<SupplyOrderItemRespVO> itemList = supplyOrderItemMapper.getAdminSupplyOrderItemList(orderId);

        if (!CollectionUtil.isEmpty(itemList)) {
            List<DictDataRespDTO> unit = dataApi.getDictDataList("unit");
            List<DictDataRespDTO> packing = dataApi.getDictDataList("packing");
            List<DictDataRespDTO> capacity = dataApi.getDictDataList("capacity");
            List<DictDataRespDTO> degree = dataApi.getDictDataList("degree");
            List<DictDataRespDTO> year = dataApi.getDictDataList("year");
            List<DictDataRespDTO> odorType = dataApi.getDictDataList("odor_type");
            List<DictDataRespDTO> language = dataApi.getDictDataList("language");
            List<DictDataRespDTO> producingArea = dataApi.getDictDataList("producing_area");

            List<SupplyOrderItemRespVO> collect = itemList.stream().peek(item -> {
                unit.forEach(dict -> {
                    if (dict.getValue().equals(String.valueOf(item.getUnit()))) {
                        item.setUnitName(dict.getLabel());
                    }
                });
                packing.forEach(dict -> {
                    if (dict.getValue().equals(String.valueOf(item.getPacking()))) {
                        item.setPackingName(dict.getLabel());
                    }
                });
                capacity.forEach(dict -> {
                    if (dict.getValue().equals(String.valueOf(item.getCapacity()))) {
                        item.setCapacityName(dict.getLabel());
                    }
                });
                degree.forEach(dict -> {
                    if (dict.getValue().equals(String.valueOf(item.getDegree()))) {
                        item.setDegreeName(dict.getLabel());
                    }
                });
                year.forEach(dict -> {
                    if (dict.getValue().equals(String.valueOf(item.getShowYear()))) {
                        item.setShowYearName(dict.getLabel());
                    }
                });


                odorType.forEach(dict -> {
                    if (dict.getValue().equals(String.valueOf(item.getFragranceType()))) {
                        item.setFragranceTypeName(dict.getLabel());
                    }
                });
                language.forEach(dict -> {
                    if (dict.getValue().equals(String.valueOf(item.getLanguage()))) {
                        item.setLanguageName(dict.getLabel());
                    }
                });
                producingArea.forEach(dict -> {
                    if (dict.getValue().equals(String.valueOf(item.getProductArea()))) {
                        item.setProductAreaName(dict.getLabel());
                    }
                });
            }).collect(Collectors.toList());
        }

        return itemList;
    }

    @Override
    public List<AppSupplyOrderItemRespVO> getSupplyOrderItemList(Long orderId) {
        return supplyOrderItemMapper.getSupplyOrderItemList(orderId);
    }

}
